﻿<%@ Page Title="" Language="C#" MasterPageFile="../../Views/Shared/Site.Master"
    Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">

<%
    var allProjectTypes = (IList<ProjectType>)ViewData["AllProjectTypes"];
     %>


<% if (false)
   { %>
<script src="jquery-vsdoc.js" type="text/javascript"></script>
<script src="jquery-ui-1.8.custom.min.js" type="text/javascript"></script>
<script src="jquery.form.js" type="text/javascript"></script>
<%} %>


<script type="text/javascript">

    function chooseMembersCallBack(selectedMembers) {
        var container = $("#teamMembersContainer").empty();
        var textarea = $('<textarea cols="40" rows="3" readonly="true"></textarea>');
        // 将选中的用户加入选中成员列表
        var members = [];
        $(selectedMembers)
        .each(function (index, item) {
            members[members.length] = item.Name;

            // 所有团队成员添加到 hiddenField
            $('<input type="hidden" name="p_teamMemberId"/>')
                .val(item.Id)
                .appendTo(container);
        });
        textarea
          .textbox()
          .val(members.join(', '))
          .appendTo(container);

        // 更改 负责人 编辑
        // 清空 负责人下拉框
        if (selectedMembers.length > 0){
            $('#teamLeaderContainer').slideDown()

            if ($('#cmbTeamMembers')){
                var selectedId = $('#cmbTeamMembers').val();
                $('#cmbTeamMembers').remove();
                }
            $('<select id="cmbTeamMembers" name="p_teamLeaderId"/>').appendTo($('#teamLeaderContainer'));


            $(selectedMembers).each(function (index, item) {
                // 添加选项
                var option = $('<option></option>').val(item.Id).html(item.Name)
                option.appendTo("#cmbTeamMembers");
                });
            // 选中当前选中项
            if (selectedId != null){
                $('#cmbTeamMembers').val(selectedId);
                }
            }
        else {
            $('#cmbTeamMembers').empty();
            $('#teamLeaderContainer').slideUp();
            }
      }


      function submitCallback(e) {
          $.ajaxBusy('end');
          $.displayInfo({
            state: e.State,
            info: e.Info
            });
          var projectId = e.Data.ProjectId; 
          if (e.State == 'Success') {
              setTimeout(function () {
                  // 跳转到查看项目页面
                  window.location.href = '<%= Url.EzDeskUrl("Project", "/ProjectDetail?projectId=' + projectId + '") %>';
              }, 1000);
          }
      }


      $(function () {

          var peopleSelector = new EzUserInfoSelector(chooseMembersCallBack);

          $('#formSubmit').ajaxForm();

          // 选择团队按钮的点击事件
          $('#btnChooseTeamMembers').click(function () {
              peopleSelector.Show();
          });

          // 创建项目提交按钮的点击事件
          $('#btnSubmit').click(function () {
              $.ajaxBusy('begin', {
                sender : this,
                isOverlay: true
              });
              $('#formSubmit').ajaxSubmit(submitCallback);
          });

      });
</script>    

    <div style="min-width: 480px;">
    <h2>创建新的项目</h2>        
    <form action="<%= Url.EzDeskUrl("Project", "/CreateProject", "ajax")%>" method="post" id="formSubmit">
    <div class="sec">
        <label for="projectName">
            项目名称：<%= Html.TextBox("p_projectName", null, new { style="width: 300px;" }) %><span class="important">*</span></label>
    </div>
    <div class="sec">
        <label for="projectNo">
            项目编号：<%= Html.TextBox("p_projectNo", null, new { style = "width: 300px;" }) %><span class="important">*</span></label>
    </div>
    <div class="sec">
        <label>
            项目类型：
        <% foreach(var item in allProjectTypes){ %>
                <label for="<%= item.OrderNo %>"><input type="radio" id="<%= item.OrderNo %>" name="p_projectType" value="<%= item.Name %>" 
                <%if (allProjectTypes.IndexOf(item) == 0) {%>checked="checked"<%} %> />
                <%= item.Name %>
                </label>
        <%} %>
        <span class="important">*</span>
        </label>
    </div>
    <div class="sec">
        <label>项目团队：
            <div id="teamMembersContainer">
                <textarea cols="40" rows="3" readonly="true"></textarea>
            </div>
            <input type="button" id="btnChooseTeamMembers" value="选择项目团队" /><span class="important">*</span>
            <div id="teamLeaderContainer" class="sec hide">
                <label>团队负责人：</label>
            </div>            
        </label>

    </div>


    <div class="sec">
        <label>
            项目备注：</label><br />
            <textarea id="remark" name="p_remark" cols="40" rows="3"></textarea>
    </div>
    <input type="button" id="btnSubmit" value="创建" />
    </form>
    </div>

</asp:Content>
